package com.正则表达式配;

public class Solution {
    public boolean match(char[] str, char[] pattern)
    {
        int i = str.length;
        int j = pattern.length;
        int sindex=0,pindex=0;
        if(i==0||j==0){
            return false;
        }
        else
            return matchcore(str,sindex,pattern,pindex);
    }
    public boolean matchcore(char[] str,int sindex,char[] pattern ,int pindex){
        if(sindex==str.length&&pindex==pattern.length){
            return true;
        }
        if(sindex<str.length&&pindex==pattern.length){
            return false;
        }
        
        //若匹配的第二个字符是*,这个要先判断
        if(pindex+1<pattern.length&&pattern[pindex+1]=='*'){
            if((sindex<str.length&&str[sindex]==pattern[pindex])||(sindex<str.length&&pattern[pindex]=='.')){
                return matchcore(str,sindex,pattern,pindex+2)||
                       matchcore(str,sindex+1,pattern,pindex);
                       
            }
            else{
                return matchcore(str,sindex,pattern,pindex+2);
            }
            
        }
      //匹配的第二个字符不是*
        if((sindex<str.length&&pattern[pindex]=='.')||(sindex<str.length&&pattern[pindex]==str[sindex])){
            return matchcore(str,sindex+1,pattern,pindex+1);
        }
        return false;
    }
}
